import urllib,urllib2,re,xbmcplugin,xbmcgui

#TV DASH - by You 2008.

def CATEGORIES():

        addLink( 'QQ','https://cdn.livestream.com/grid/LSPlayer.swf?channel=avenavo&fb_version=2.0&autoPlay=true','http://bestplayer.tv/okladki/f9f67e82dc414f1a28eae6a1b7877894_thumb.jpg')
        addLink( 'Matrix','http://s117.maxvideo.pl/stream/8fd8807132ef6e1b6bf3035afd94f7161f394a0108341b76323c33b4ec3d1080/x/file.flv?start=0','http://bestplayer.tv/okladki/c4/c43e5b54f8ed936e1c6a41bde6aa0654_thumb.jpg')
        addLink( 'Niesamowity Spiderman','http://s3.maxvideo.pl/stream/eeec6bf5965a040fd2b54f069e2c97e5375732a1cbcd4be0af394e682a458ba3/x/file.flv?start=0','http://bestplayer.tv/okladki/81/81ab4bdf0b7abaf569afb731798578e1_thumb.jpg')
        addLink( 'Trzynastu/13','http://ds10.maxvideo.pl:8118/23b90d9cacf8478a58ec05ad04cd9f3ba4d643c60d1fd68b1967607a0a757937/x/file.flv?start=0','http://bestplayer.tv/okladki/2b/2bc1a4238f50e710e4c63b8e3e577141_thumb.jpg')
        
        
         

def INDEX(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
        match=re.compile('src=".+?"><p><a\r\n        href=".+?">.+?</a>').findall(link)
        for thumbnail,url,name in match:
                addDir(name,url,2,thumbnail)

def VIDEOLINKS(url,name):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
        match=re.compile('<embed src=".+?" type=".+?"').findall(link)
        for url in match:
                addLink(name,url,)
                
def ajoin(arr):
    strtest = ''
    for num in range(len(arr)):
        strtest = strtest + str(arr[num])
    return strtest

def asplit(mystring):
    arr = []
    for num in range(len(mystring)):
        arr.append(mystring[num])
    return arr
        

                
def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]
                                
        return param




def addLink(name,url,iconimage):
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz)
        return ok


def addDir(name,url,mode,iconimage):
        u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
        return ok
        
              
params=get_params()
url=None
name=None
mode=None

try:
        url=urllib.unquote_plus(params["url"])
except:
        pass
try:
        name=urllib.unquote_plus(params["name"])
except:
        pass
try:
        mode=int(params["mode"])
except:
        pass

print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)

if mode==None or url==None or len(url)<1:
        print ""
        CATEGORIES()
       
elif mode==1:
        print ""+url
        INDEX(url)
        
elif mode==2:
        print ""+url
        VIDEOLINKS(url,name)



xbmcplugin.endOfDirectory(int(sys.argv[1]))
